package com.ipbd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipbd.domain.CostBenefit;
import com.ipbd.domain.ThoughtRecord;
import com.ipbd.dto.UserDTO;
import com.ipbd.mapper.CostBenefitMapper;
import com.ipbd.service.CostBenefitService;
import com.ipbd.utils.UserHolder;
import com.ipbd.vo.CostBenefitAllVo;
import com.ipbd.vo.CostBenefitVo;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 86172
 * @description 针对表【cost_benefit】的数据库操作Service实现
 * @createDate 2024-03-06 20:53:48
 */
@Service
public class CostBenefitServiceImpl extends ServiceImpl<CostBenefitMapper, CostBenefit>
        implements CostBenefitService {




    @Override
    public List<CostBenefitVo> getCostBenefits() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 获取当天日期
        LocalDate today = LocalDate.now();
        // 构建查询条件，根据当前用户id查询记录
        QueryWrapper<CostBenefit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("record_date", today);
        List<CostBenefit> costBenefits = baseMapper.selectList(queryWrapper);

        // 转换为 CostBenefitVo 对象列表
        List<CostBenefitVo> costBenefitVoList = new ArrayList<>();
        for (CostBenefit costBenefit : costBenefits) {
            CostBenefitVo costBenefitVo = new CostBenefitVo();
            costBenefitVo.setObject(costBenefit.getObject());
            costBenefitVo.setDisadvantages(costBenefit.getDisadvantages());
            costBenefitVo.setAdvantages(costBenefit.getAdvantages());
            costBenefitVoList.add(costBenefitVo);
        }
        return costBenefitVoList;
    }



    @Override
    public void saveCostBenefit(CostBenefitVo costBenefitVo) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 获取当天日期
        LocalDate today = LocalDate.now();

        CostBenefit costBenefit = new CostBenefit();
        costBenefit.setUserId(id);
        costBenefit.setRecordDate(Date.valueOf(today));
        costBenefit.setObject(costBenefitVo.getObject());
        costBenefit.setDisadvantages(costBenefitVo.getDisadvantages());
        costBenefit.setAdvantages(costBenefitVo.getAdvantages());
        ;

        // 插入数据库
        baseMapper.insert(costBenefit);
    }
}




